package com.ziqing.apeletnews.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.utils.BarUtils;
import com.blankj.utilcode.utils.KeyboardUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.baseMvp.BasePresenter;
import com.ziqing.apeletnews.baseMvp.BaseView;
import com.ziqing.apeletnews.utils.AppManager;
import com.ziqing.apeletnews.utils.SystemTypeUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


import butterknife.ButterKnife;

import static android.view.KeyCharacterMap.ALPHA;

/**
 * Created by 郑谊庄 on 2017/5/23.
 */

public abstract class BaseActivity <V, T extends BasePresenter<V>> extends RxAppCompatActivity implements BaseView {
    protected Toolbar toolbar;
    protected T mPresenter ;

    /**
     * 创建Presenter
     * @return
     */
    protected abstract T createPresenter();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 允许为空，不是所有页面都是需要实现mvp模式的
         * 不为空时，为view创建一个弱引用
         */

        EventBus.getDefault().register(this);
        AppManager.getAppManager().addActivity(this);
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        ImmersionBar.with(this)
                .statusBarColor(R.color.word_white)
                .statusBarDarkFont(true)
                .init();
        mPresenter = createPresenter() ;
        if (mPresenter != null){
            mPresenter.attachView((V) this);
        }


//        if (presenter == null) {
//            throw new NullPointerException("Presenter is null! Do you return null in createPresenter()?");
//        }
//        presenter.onMvpAttachView(this, savedInstanceState);
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title) {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(title);
    }

//    /**
//     * 初始化toolbar(返回).
//     * @param backDra
//     */
//    public void setUpToolbar(int backDra){
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
//
//        toolbar.setNavigationIcon(backDra);
//        toolbar.setNavigationOnClickListener(view -> {
//            KeyboardUtils.hideSoftInput(this);
//            finish();
//        });
//    }
    public void setUpToolbar(int backDra){
        ImageView iv_left = findViewById(R.id.iv_left);
        iv_left.setImageResource(backDra);
        iv_left.setOnClickListener(view -> {
                    KeyboardUtils.hideSoftInput(this);
                    finish();
                });
    }

    public void setRightToolbar(int backDra){
        ImageView iv_left = findViewById(R.id.iv_right);
        iv_left.setImageResource(backDra);
        iv_left.setOnClickListener(view -> {
            KeyboardUtils.hideSoftInput(this);
            finish();
        });
    }

    /**
     *设置状态栏颜色为深色
     * @param isDark
     * @return
     */

    public boolean setStatusBarMode(boolean isDark) {
        Window window = getWindow();
        return SystemTypeUtil.setStatusBarLightMode(window, isDark);
    }

    /**
     * 设置沉浸.标题颜色
     */
    public void setBarColor(@ColorRes int color) {
        BarUtils.setColor(this, ContextCompat
                .getColor(this, color), ALPHA);
        if (toolbar != null) {
            toolbar.setBackgroundColor(getResources().getColor(color));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void exitAll(FinishAll all) {
        finish();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }


    protected void initView() {

    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife(ActivityEvent activityEvent) {
        return this.<T>bindUntilEvent(activityEvent);
    }
    @Override
    protected void onResume() {
        super.onResume();
        if (mPresenter != null){
            mPresenter.attachView((V) this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //清空presenter对view的引用
        if (mPresenter != null){
            mPresenter.detachView();
        }
    }
    /**
     * 不带参数，从Activity跳转到指定Activity
     *
     * @param activity
     */
    protected void toActivity(Class<?> activity){
        startActivity(new Intent(this , activity));
    }
}
